package com.bwc.aidemo.paozhong

import android.os.Handler
import android.os.Looper
import android.speech.tts.TextToSpeech
import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.bwc.aidemo.R
import com.bwc.aidemo.arouter.RoutePath
import com.bwc.aidemo.base.BaseActivity
import com.bwc.aidemo.databinding.ActivityPaozhongBinding
import com.bwc.aidemo.http.ApiClients
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import com.google.gson.annotations.SerializedName
import com.mojawa.mj_util.permiss.PermissionUtils
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.Locale




class PaozhongActivity : BaseActivity<ActivityPaozhongBinding>(), TextToSpeech.OnInitListener {

    private val tts: TextToSpeech? = null


    override fun getLayoutId(): Int {
        return R.layout.activity_paozhong
    }

    override fun initViews() {
        dataBinding.btnStartRunning.setOnClickListener {
            PermissionUtils.checkLocationPermission(this) {
                ARouter.getInstance().build(RoutePath.Amap).withBoolean("target",false).navigation()
            }
        }
    }

    override fun initData() {
//        getVoice()

    }
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionUtils.handlePermissionsResult(requestCode, permissions, grantResults, this)
    }



    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onInit(status: Int) {
        if (status == TextToSpeech.SUCCESS) {
            val result = tts!!.setLanguage(Locale.US) // 设置语言为美国英语
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.d("TAG", "onInit: 语言不支持")
            } else {
                speak("Hello, this is a text-to-speech example.") // 开始说话
            }
        } else {
            Log.d("TAG", "onInit: 失败")
        }
    }

    private fun speak(text: String) {
        tts!!.speak(text, TextToSpeech.QUEUE_FLUSH, null, null) // 开始说话，并清空队列中之前的语音
    }

}

data class CozeApiResponse(
    val code: Int,
    val msg: String,
    val detail: DetailBean,
    val usage: UsageBean,
    val data: String // 内层 JSON 字符串
)

// 2. detail 和 usage 子项（与之前一致）
data class DetailBean(val logid: String)
data class UsageBean(
    val token_count: Int,
    val output_count: Int,
    val input_count: Int
)

// 3. 内层 data 解析类（注意：此时 data 是单个字符串，非数组）
data class SportContentData(
    @SerializedName("content_type") val contentType: Int,
    @SerializedName("original_result") val originalResult: String?,
    @SerializedName("type_for_model") val typeForModel: Int,
    val data: String // 单个提示文本（如"锲而舍之，朽木不折；..."）
)